<!doctype html>



  


<html class="theme-next muse use-motion">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  
    
      
    

    
  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Microsoft YaHei:300,300italic,400,400italic,700,700italic|consolas:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.2" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="js,面向对象," />








  <link rel="shortcut icon" type="image/x-icon" href="http://ofy7k3v4s.bkt.clouddn.com/mylogo.png?v=5.0.2" />






<meta name="description" content="&amp;#x9762;&amp;#x5411;&amp;#x5BF9;&amp;#x8C61;&amp;#x7684;&amp;#x8BED;&amp;#x8A00;&amp;#x7684;&amp;#x6807;&amp;#x5FD7;&amp;#x5C31;&amp;#x662F;&amp;#x5B83;&amp;#x4EEC;&amp;#x6709;&amp;#x7C7B;&amp;#x7684;&amp;#x6982;&amp;#x5FF5;&amp;#xFF0C;&amp;#x901A;&amp;#x8FC7;&amp;#x7C7B;&amp;#x53EF;&amp;#x4EE5;">
<meta property="og:type" content="article">
<meta property="og:title" content="面向对象的程序设计">
<meta property="og:url" content="https://hongqinma.github.io/2014/06/03/11-面向对象的程序设计/index.html">
<meta property="og:site_name" content="HongqinMa' Blog">
<meta property="og:description" content="&amp;#x9762;&amp;#x5411;&amp;#x5BF9;&amp;#x8C61;&amp;#x7684;&amp;#x8BED;&amp;#x8A00;&amp;#x7684;&amp;#x6807;&amp;#x5FD7;&amp;#x5C31;&amp;#x662F;&amp;#x5B83;&amp;#x4EEC;&amp;#x6709;&amp;#x7C7B;&amp;#x7684;&amp;#x6982;&amp;#x5FF5;&amp;#xFF0C;&amp;#x901A;&amp;#x8FC7;&amp;#x7C7B;&amp;#x53EF;&amp;#x4EE5;">
<meta property="og:updated_time" content="2016-11-21T04:01:04.617Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="面向对象的程序设计">
<meta name="twitter:description" content="&amp;#x9762;&amp;#x5411;&amp;#x5BF9;&amp;#x8C61;&amp;#x7684;&amp;#x8BED;&amp;#x8A00;&amp;#x7684;&amp;#x6807;&amp;#x5FD7;&amp;#x5C31;&amp;#x662F;&amp;#x5B83;&amp;#x4EEC;&amp;#x6709;&amp;#x7C7B;&amp;#x7684;&amp;#x6982;&amp;#x5FF5;&amp;#xFF0C;&amp;#x901A;&amp;#x8FC7;&amp;#x7C7B;&amp;#x53EF;&amp;#x4EE5;">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '6347850919329138000',
      author: 'Hiraku'
    }
  };
</script>




  <link rel="canonical" href="https://hongqinma.github.io/2014/06/03/11-面向对象的程序设计/"/>


  <title> 面向对象的程序设计 | HongqinMa' Blog </title>
</head>

<body itemscope itemtype="//schema.org/WebPage" lang="en">

  



  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?10010323";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>








  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="//schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta custom-logo">
  
    <div class="site-meta-headline">
      <a>
        <img class="custom-logo-image" src="http://ofy7k3v4s.bkt.clouddn.com/photo.jpg"
             alt="HongqinMa' Blog"/>
      </a>
    </div>
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">HongqinMa' Blog</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">Spell at all costs， to rush you in the future。</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            Categories
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            About
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            Tags
          </a>
        </li>
      

      
    </ul>
  

  
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="//schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                面向对象的程序设计
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">Posted on</span>
            <time itemprop="dateCreated" datetime="2014-06-03T23:33:08+08:00" content="2014-06-03">
              2014-06-03
            </time>
          </span>

          
            <span class="post-category" >
              &nbsp; | &nbsp;
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">In</span>
              
                <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
                  <a href="/categories/JavaScript/" itemprop="url" rel="index">
                    <span itemprop="name">JavaScript</span>
                  </a>
                </span>

                
                

              
            </span>
          

          
            
              <span class="post-comments-count">
                &nbsp; | &nbsp;
                <a href="/2014/06/03/11-面向对象的程序设计/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="2014/06/03/11-面向对象的程序设计/" itemprop="commentsCount"></span>
                </a>
              </span>
            
          

          

          

          
              &nbsp; | &nbsp;
              <span class="page-pv"><i class="fa fa-file-o"></i>
              <span class="busuanzi-value" id="busuanzi_value_page_pv" ></span>
              </span>
          

          
            <span id="busuanzi_container_page_pv">  <span id="busuanzi_value_page_pv"></span> Views</span>
          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>&#x9762;&#x5411;&#x5BF9;&#x8C61;&#x7684;&#x8BED;&#x8A00;&#x7684;&#x6807;&#x5FD7;&#x5C31;&#x662F;&#x5B83;&#x4EEC;&#x6709;&#x7C7B;&#x7684;&#x6982;&#x5FF5;&#xFF0C;&#x901A;&#x8FC7;&#x7C7B;&#x53EF;&#x4EE5;&#x521B;&#x5EFA;&#x4EFB;&#x610F;&#x591A;&#x4E2A;&#x5177;&#x6709;&#x76F8;&#x540C;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;&#x7684;&#x5BF9;&#x8C61;&#x3002;JavaScript &#x4E2D;&#x7684;&#x7C7B;&#x7684;&#x5B9E;&#x73B0;&#x662F;&#x57FA;&#x4E8E;&#x5176;&#x539F;&#x578B;&#x7EE7;&#x627F;&#x673A;&#x5236;&#x3002;&#x5982;&#x679C;&#x4E24;&#x4E2A;&#x5B9E;&#x4F8B;&#x90FD;&#x4ECE;&#x540C;&#x4E00;&#x4E2A;&#x539F;&#x578B;&#x5BF9;&#x8C61;&#x4E0A;&#x7EE7;&#x627F;&#x4E86;&#x5C5E;&#x6027;&#xFF0C;&#x5C31;&#x8BA4;&#x4E3A;&#x662F;&#x540C;&#x4E00;&#x4E2A;&#x7C7B;&#x7684;&#x5B9E;&#x4F8B;&#x3002;</p>
<p>JavaScript &#x7684;&#x5BF9;&#x8C61;&#x662F;&#x5C5E;&#x6027;&#x540D;&#x4EE5;&#x53CA;&#x4E0E;&#x4E4B;&#x5BF9;&#x5E94;&#x7684;&#x503C;&#x7684;&#x57FA;&#x672C;&#x96C6;&#x5408;&#x3002;&#x96C6;&#x5408;&#x662F;&#x4E00;&#x79CD;&#x6570;&#x636E;&#x7ED3;&#x6784;&#xFF0C;&#x6CF3;&#x8863;&#x8868;&#x793A;&#x975E;&#x91CD;&#x590D;&#x503C;&#x7684;&#x65E0;&#x5E8F;&#x96C6;&#x5408;&#x3002;</p>
<h2 id="&#x7406;&#x89E3;&#x5BF9;&#x8C61;"><a href="#&#x7406;&#x89E3;&#x5BF9;&#x8C61;" class="headerlink" title="&#x7406;&#x89E3;&#x5BF9;&#x8C61;"></a>&#x7406;&#x89E3;&#x5BF9;&#x8C61;</h2><p>JavaScript &#x662F;&#x4E00;&#x95E8;&#x57FA;&#x4E8E;&#x5BF9;&#x8C61;&#x7684;&#x591A;&#x6CDB;&#x5F0F;&#x8BED;&#x8A00;&#x3002;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x9762;&#x5411;&#x8FC7;&#x7A0B;&#x8FDB;&#x884C;&#x5F00;&#x53D1;&#xFF1A;</p>
<ul>
<li>&#x83B7;&#x53D6;&#x5143;&#x7D20;&#xFF0C;&#x7ED1;&#x5B9A;&#x4E8B;&#x4EF6;&#x3001;&#x8BBE;&#x7F6E;&#x6837;&#x5F0F;&#x3001;&#x5B8C;&#x6210;&#x52A8;&#x753B;&#x3002;&#x3002;&#x3002;&#x3002;&#x3002;&#x3002;</li>
</ul>
<p>&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x9762;&#x5411;&#x5BF9;&#x8C61;&#x7684;&#x65B9;&#x5F0F;&#x8FDB;&#x884C;&#x5F00;&#x53D1;&#xFF1A;</p>
<ul>
<li>&#x9762;&#x5411;&#xFF08;&#x5173;&#x6CE8;&#x4E8E;&#xFF09;&#x8FC7;&#x7A0B;&#xFF1A;&#x57FA;&#x4E8E;&#x51FD;&#x6570;&#xFF0C;&#x5C01;&#x88C5;&#x51FD;&#x6570;</li>
<li>&#x9762;&#x5411;&#x5BF9;&#x8C61;&#xFF1A;&#x5173;&#x6CE8;&#x70B9;&#x53D8;&#x6210;&#x4E86;&#x5BF9;&#x8C61;</li>
<li>&#x5BF9;&#x8C61;&#x7684;&#x6982;&#x5FF5;&#xFF1A;&#x6570;&#x636E;&#x96C6;&#xFF0C;&#x529F;&#x80FD;&#x96C6;&#xFF1A; &#x65E0;&#x5E8F;&#x5C5E;&#x6027;&#x7684;&#x96C6;&#x5408;&#xFF0C;&#x5305;&#x542B;&#x57FA;&#x672C;&#x503C;&#xFF0C;&#x5BF9;&#x8C61;&#x6216;&#x8005;&#x51FD;&#x6570;&#x3002;</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//CEO&#xFF1A;&#x5B89;&#x6392;&#x4E00;&#x4E2A;&#x4EFB;&#x52A1;&#x7ED9;CTO(7&#x5929;)&#xFF0C;CTO&#x53C8;&#x628A;&#x4EFB;&#x52A1;&#x7ED9;PM(5&#x5929;)&#xFF0C;PM&#x53C8;&#x628A;&#x4EFB;&#x52A1;&#x7ED9;&#x6211;&#x4E86;(3&#x5929;)&#xFF0C;&#x6211;&#x53BB;&#x5F00;&#x53D1;&#x8FD9;&#x4E2A;&#x9875;&#x9762;</span></div><div class="line"><span class="keyword">var</span> ceo = {</div><div class="line">    <span class="attr">assignTaskToCTO</span>: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{ <span class="built_in">console</span>.log(<span class="string">&quot;&#x5B89;&#x6392;&#x4E00;&#x4E2A;&#x4EFB;&#x52A1;&#x7ED9;CTO&quot;</span>); }</div><div class="line">};</div><div class="line"><span class="keyword">var</span> cto = {</div><div class="line">    <span class="attr">assignTaskToPM</span>: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{ <span class="built_in">console</span>.log(<span class="string">&quot;&#x5B89;&#x6392;&#x4E00;&#x4E2A;&#x4EFB;&#x52A1;&#x7ED9;PM&quot;</span>);}</div><div class="line">};</div><div class="line"><span class="keyword">var</span> pm = {</div><div class="line">    <span class="attr">assignTaskToMe</span>: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{ <span class="built_in">console</span>.log(<span class="string">&quot;&#x5B89;&#x6392;&#x4E00;&#x4E2A;&#x4EFB;&#x52A1;&#x7ED9;&#x6211;&quot;</span>); }</div><div class="line">};</div><div class="line"><span class="keyword">var</span> me = {</div><div class="line">    <span class="attr">developWeb</span>:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{ <span class="built_in">console</span>.log(<span class="string">&quot;&#x6211;&#x53BB;&#x5F00;&#x53D1;&#x8FD9;&#x4E2A;&#x9875;&#x9762;&quot;</span>); }</div><div class="line">};</div><div class="line"><span class="comment">//&#x5F00;&#x53D1;&#x4E00;&#x4E2A;&#x9875;&#x9762;</span></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">deleveWeb</span>(<span class="params"></span>)</span>{</div><div class="line">    ceo.assignTaskToCTO();</div><div class="line">    cto.assignTaskToPM();</div><div class="line">    pm.assignTaskToMe();</div><div class="line">    me.developWeb();</div><div class="line">}</div></pre></td></tr></table></figure>
<p>&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;&#xFF1A; &#x5229;&#x7528;&#x6784;&#x9020;&#x51FD;&#x6570;&#x6765;&#x5B9A;&#x4E49; &#x201C;&#x8303;&#x56F4;&#x7C7B;&#x201D;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div></pre></td><td class="code"><pre><div class="line">  <span class="comment">// &#x6784;&#x9020;&#x51FD;&#x6570;&#x521D;&#x59CB;&#x5316;</span></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">Range</span>(<span class="params">from, to</span>) </span>{</div><div class="line">  <span class="comment">// &#x5B58;&#x50A8;&#x8D77;&#x59CB;&#x4F4D;&#x7F6E;&#x548C;&#x7ED3;&#x675F;&#x4F4D;&#x7F6E;</span></div><div class="line">  <span class="keyword">this</span>.from = <span class="keyword">from</span>;</div><div class="line">  <span class="keyword">this</span>.to = to;</div><div class="line">}</div><div class="line">Range.prototype = {</div><div class="line">  <span class="comment">// constructor: Range,</span></div><div class="line">  <span class="comment">// &#x5982;&#x679C; x &#x5728;&#x8303;&#x56F4;&#x5185;&#xFF0C;&#x8FD4;&#x56DE; true&#xFF0C;&#x5426;&#x5219;&#x8FD4;&#x56DE; false</span></div><div class="line">  <span class="comment">// &#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x6BD4;&#x8F83;&#x6570;&#x5B57;&#x8303;&#x56F4;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x6BD4;&#x8F83;&#x5B57;&#x7B26;&#x4E32;&#x548C;&#x65E5;&#x671F;&#x8303;&#x56F4;</span></div><div class="line">  includes: <span class="function"><span class="keyword">function</span>(<span class="params">x</span>) </span>{ <span class="keyword">return</span> <span class="keyword">this</span>.from &lt;= x &amp;&amp; x &lt;= <span class="keyword">this</span>.to},</div><div class="line">  <span class="comment">// &#x5BF9;&#x4E8E;&#x8303;&#x56F4;&#x5185;&#x7684;&#x6BCF;&#x4E00;&#x4E2A;&#x6574;&#x6570;&#x8C03;&#x7528;&#x51FD;&#x6570; f</span></div><div class="line">  foreach: <span class="function"><span class="keyword">function</span>(<span class="params">f</span>) </span>{</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> x = <span class="built_in">Math</span>.ceil(<span class="keyword">this</span>.from); x &lt;= <span class="keyword">this</span>.to; x++) f(x);</div><div class="line">  },</div><div class="line">  <span class="comment">// &#x8FD4;&#x56DE;&#x8868;&#x793A;&#x8FD9;&#x4E2A;&#x8303;&#x56F4;&#x7684;&#x5B57;&#x7B26;&#x4E32;</span></div><div class="line">  toString: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>{ <span class="keyword">return</span> <span class="string">&quot;(&quot;</span> + <span class="keyword">this</span>.from + <span class="string">&quot;...&quot;</span> + <span class="keyword">this</span>.to + <span class="string">&quot;)&quot;</span>; }</div><div class="line">};</div><div class="line"></div><div class="line"><span class="comment">// &#x4E3E;&#x4F8B;</span></div><div class="line"><span class="keyword">var</span> r = <span class="keyword">new</span> Range(<span class="number">3</span>, <span class="number">5</span>); </div><div class="line"><span class="built_in">console</span>.log(r.includes(<span class="number">4</span>)); <span class="comment">// true</span></div><div class="line">r.foreach(<span class="built_in">console</span>.log); <span class="comment">// 3, 4, 5</span></div><div class="line"><span class="built_in">console</span>.log(r);  <span class="comment">// Range{from:3, to:5}</span></div></pre></td></tr></table></figure>
<h2 id="&#x6784;&#x9020;&#x51FD;&#x6570;&#x548C;&#x7C7B;&#x7684;&#x6807;&#x8BC6;"><a href="#&#x6784;&#x9020;&#x51FD;&#x6570;&#x548C;&#x7C7B;&#x7684;&#x6807;&#x8BC6;" class="headerlink" title="&#x6784;&#x9020;&#x51FD;&#x6570;&#x548C;&#x7C7B;&#x7684;&#x6807;&#x8BC6;"></a>&#x6784;&#x9020;&#x51FD;&#x6570;&#x548C;&#x7C7B;&#x7684;&#x6807;&#x8BC6;</h2><p>&#x5F53;&#x4E14;&#x4EC5;&#x5F53;&#x4E24;&#x4E2A;&#x5BF9;&#x8C61;&#x7EE7;&#x627F;&#x81EA;&#x540C;&#x4E00;&#x4E2A;&#x539F;&#x578B;&#x5BF9;&#x8C61;&#x65F6;&#xFF0C;&#x5B83;&#x4EEC;&#x624D;&#x662F;&#x5C5E;&#x4E8E;&#x540C;&#x4E00;&#x4E2A;&#x7C7B;&#x7684;&#x5B9E;&#x4F8B;&#x3002;&#x521D;&#x59CB;&#x5316;&#x5BF9;&#x8C61;&#x7684;&#x72B6;&#x6001;&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#x4E0D;&#x80FD;&#x4F5C;&#x4E3A;&#x7C7B;&#x7684;&#x6807;&#x8BC6;&#xFF0C;&#x4E24;&#x4E2A;&#x6784;&#x9020;&#x51FD;&#x6570;&#x7684; prototype &#x5C5E;&#x6027;&#x53EF;&#x80FD;&#x6307;&#x5411;&#x540C;&#x4E00;&#x4E2A;&#x539F;&#x578B;&#x5BF9;&#x8C61;&#x3002;&#x90A3;&#x4E48;&#x8FD9;&#x8FDE;&#x4E2A;&#x6784;&#x9020;&#x51FD;&#x6570;&#x7684;&#x5B9E;&#x4F8B;&#x662F;&#x5C5E;&#x4E8E;&#x540C;&#x4E00;&#x7C7B;&#x578B;&#x7684;&#x3002;</p>
<p>&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>r instanceof Range</code> &#x6765;&#x5224;&#x65AD;&#x4E00;&#x4E2A;&#x5B9E;&#x4F8B;&#x662F;&#x5426;&#x7EE7;&#x627F;&#x81EA; Range.prototype&#x3002;</p>
<h2 id="constructor-&#x5C5E;&#x6027;"><a href="#constructor-&#x5C5E;&#x6027;" class="headerlink" title="constructor &#x5C5E;&#x6027;"></a>constructor &#x5C5E;&#x6027;</h2><p>&#x6BCF;&#x4E00;&#x4E2A; JavaScript &#x51FD;&#x6570;&#x90FD;&#x81EA;&#x52A8;&#x62E5;&#x6709;&#x4E00;&#x4E2A; prototype &#x5C5E;&#x6027;&#xFF0C;&#x8FD9;&#x4E2A;&#x5C5E;&#x6027;&#x6307;&#x5411;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x5373;&#x79F0;&#x4F5C;&#x539F;&#x578B;&#x5BF9;&#x8C61;&#xFF0C;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x5305;&#x542B;&#x552F;&#x4E00;&#x4E00;&#x4E2A;&#x4E0D;&#x53EF;&#x679A;&#x4E3E;&#x7684;&#x5C5E;&#x6027; constructor&#x3002; constructor &#x7684;&#x503C;&#x662F;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x5BF9;&#x8C61;&#x3002;</p>
<p>&#x6784;&#x9020;&#x51FD;&#x6570;&#x7684;&#x539F;&#x578B;&#x4E2D;&#x5B58;&#x5728;&#x9884;&#x5148;&#x5B9A;&#x4E49;&#x597D;&#x7684; constructor &#x5C5E;&#x6027;&#xFF0C;&#x8FD9;&#x610F;&#x5473;&#x7740;&#x901A;&#x5E38;&#x7EE7;&#x627F;&#x7684; constructor &#x5747;&#x6307;&#x4EE3;&#x5B83;&#x4EEC;&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#x3002;</p>
<p>&#x5728;&#x4E0A;&#x9762;&#x5B9A;&#x4E49;&#x7684; Range &#x6784;&#x9020;&#x51FD;&#x6570;&#x7684;&#x539F;&#x578B;&#x7531;&#x4E8E;&#x88AB;&#x53E6;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x66FF;&#x6362;&#x4E86;&#xFF0C;&#x6240;&#x4EE5;&#x91CD;&#x5199;&#x4E86;&#x9884;&#x5B9A;&#x4E49;&#x7684; Range.prototype &#x5BF9;&#x8C61;&#x3002; Range &#x7684;&#x5B9E;&#x4F8B;&#x7684; constructor &#x6CA1;&#x6709;&#x7EE7;&#x627F;&#x81EA; Range.prototype &#x7684; constructor &#x5C5E;&#x6027;&#x3002;&#x5176;&#x503C;&#x53D8;&#x6210;&#x4E86; Object&#x3002;</p>
<h2 id="&#x521B;&#x5EFA;&#x5BF9;&#x8C61;&#x7684;&#x51E0;&#x79CD;&#x65B9;&#x5F0F;"><a href="#&#x521B;&#x5EFA;&#x5BF9;&#x8C61;&#x7684;&#x51E0;&#x79CD;&#x65B9;&#x5F0F;" class="headerlink" title="&#x521B;&#x5EFA;&#x5BF9;&#x8C61;&#x7684;&#x51E0;&#x79CD;&#x65B9;&#x5F0F;"></a>&#x521B;&#x5EFA;&#x5BF9;&#x8C61;&#x7684;&#x51E0;&#x79CD;&#x65B9;&#x5F0F;</h2><p>&#x4F7F;&#x7528; Object &#x6784;&#x9020;&#x51FD;&#x6570;&#x6216;&#x5BF9;&#x8C61;&#x5B57;&#x9762;&#x91CF;&#x90FD;&#x53EF;&#x4EE5;&#x7528;&#x6765;&#x521B;&#x5EFA;&#x5355;&#x4E2A;&#x5BF9;&#x8C61;&#xFF0C;&#x4F46;&#x8FD9;&#x4E9B;&#x65B9;&#x5F0F;&#x6709;&#x4E2A;&#x660E;&#x663E;&#x7684;&#x7F3A;&#x70B9;&#xFF1A; &#x4F7F;&#x7528;&#x4E00;&#x4E2A;&#x63A5;&#x53E3;&#x521B;&#x5EFA;&#x5F88;&#x591A;&#x5BF9;&#x8C61;&#x4F1A;&#x4EA7;&#x751F;&#x5927;&#x91CF;&#x7684;&#x91CD;&#x590D;&#x4EE3;&#x7801;&#x3002;</p>
<h3 id="&#x5DE5;&#x5382;&#x6A21;&#x5F0F;"><a href="#&#x5DE5;&#x5382;&#x6A21;&#x5F0F;" class="headerlink" title="&#x5DE5;&#x5382;&#x6A21;&#x5F0F;"></a>&#x5DE5;&#x5382;&#x6A21;&#x5F0F;</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// &#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5305;&#x542B;&#x6240;&#x6709;&#x5FC5;&#x8981;&#x4FE1;&#x606F;&#x7684; Person &#x5BF9;&#x8C61;</span></div><div class="line"><span class="comment">// &#x53EF;&#x4EE5;&#x65E0;&#x6570;&#x6B21;&#x7684;&#x8C03;&#x7528;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x800C;&#x4E14;&#x6BCF;&#x6B21;&#x90FD;&#x4F1A;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x5305;&#x542B;&#x4E09;&#x4E2A;&#x5C5E;&#x6027;&#x7684;&#x65B9;&#x6CD5;&#x7684;&#x5BF9;&#x8C61;</span></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">createPerson</span>(<span class="params">name, age, job</span>) </span>{</div><div class="line">  <span class="keyword">var</span> o = <span class="keyword">new</span> <span class="built_in">Object</span>();</div><div class="line">  o.name = name;</div><div class="line">  o.age = age;</div><div class="line">  o.job = job;</div><div class="line">  o.sayName = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>{</div><div class="line">    <span class="built_in">console</span>.log(<span class="keyword">this</span>.name);</div><div class="line">  }</div><div class="line">  <span class="keyword">return</span> o;</div><div class="line">}</div><div class="line"><span class="keyword">var</span> p1 = createPerson(<span class="string">&quot;Hiraku&quot;</span>, <span class="number">23</span>, <span class="string">&quot;JavaScript Engineer&quot;</span>);</div><div class="line"><span class="keyword">var</span> p2 = createPerson(<span class="string">&quot;Wang&quot;</span>, <span class="number">22</span>, <span class="string">&quot;Java Engineer&quot;</span>);</div><div class="line">p1.sayName();</div><div class="line">p2.sayName();</div></pre></td></tr></table></figure>
<p>&#x95EE;&#x9898;&#xFF1A; &#x5E76;&#x6CA1;&#x6709;&#x89E3;&#x51B3;&#x5BF9;&#x8C61;&#x8BC6;&#x522B;&#x7684;&#x95EE;&#x9898;&#xFF08;&#x5373;&#x600E;&#x6837;&#x77E5;&#x9053;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;&#x7C7B;&#x578B;&#xFF09;&#x3002;</p>
<h3 id="&#x6784;&#x9020;&#x51FD;&#x6570;&#x6A21;&#x5F0F;"><a href="#&#x6784;&#x9020;&#x51FD;&#x6570;&#x6A21;&#x5F0F;" class="headerlink" title="&#x6784;&#x9020;&#x51FD;&#x6570;&#x6A21;&#x5F0F;"></a>&#x6784;&#x9020;&#x51FD;&#x6570;&#x6A21;&#x5F0F;</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">Person</span>(<span class="params">name, age, job</span>) </span>{</div><div class="line">  <span class="keyword">this</span>.name = name;</div><div class="line">  <span class="keyword">this</span>.age = age;</div><div class="line">  <span class="keyword">this</span>.job = job;</div><div class="line">  <span class="keyword">this</span>.sayName = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>{</div><div class="line">    <span class="built_in">console</span>.log(<span class="keyword">this</span>.name);</div><div class="line">  };</div><div class="line">}</div><div class="line"><span class="keyword">var</span> p1 = <span class="keyword">new</span> Person(<span class="string">&quot;Hiraku&quot;</span>, <span class="number">23</span>, <span class="string">&quot;JavaScript Engineer&quot;</span>);</div><div class="line"><span class="keyword">var</span> p2 = <span class="keyword">new</span> Person(<span class="string">&quot;Wang&quot;</span>, <span class="number">22</span>, <span class="string">&quot;Java Engineer&quot;</span>);</div><div class="line">p1.sayName();</div><div class="line">p2.sayName();</div></pre></td></tr></table></figure>
<p>&#x548C;&#x5DE5;&#x5382;&#x6A21;&#x5F0F;&#x7684;&#x533A;&#x522B;&#xFF1A;</p>
<ul>
<li>&#x6CA1;&#x6709;&#x663E;&#x5F0F;&#x7684;&#x521B;&#x5EFA;&#x5BF9;&#x8C61;&#xFF1B;</li>
<li>&#x76F4;&#x63A5;&#x5C06;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;&#x8D4B;&#x503C;&#x7ED9;&#x4E86; this &#x5BF9;&#x8C61;</li>
<li>&#x6CA1;&#x6709; return &#x8BED;&#x53E5;</li>
<li>&#x4F7F;&#x7528; new &#x5173;&#x952E;&#x5B57;&#x521B;&#x5EFA;&#x5BF9;&#x8C61;&#x7684;&#x5B9E;&#x4F8B;</li>
<li>&#x5C06;&#x6784;&#x9020;&#x51FD;&#x6570;&#x7684;&#x4F5C;&#x7528;&#x57DF;&#x8D4B;&#x503C;&#x7ED9;&#x65B0;&#x5BF9;&#x8C61;&#xFF08;&#x56E0;&#x6B64; this &#x5C31;&#x6307;&#x5411;&#x4E86;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#xFF09;</li>
<li>&#x8FD4;&#x56DE;&#x65B0;&#x5BF9;&#x8C61;</li>
<li>p1 &#x548C; p2 &#x6709;&#x7EE7;&#x627F;&#x81EA; Person.prototype &#x7684;&#x5C5E;&#x6027; constructor&#xFF0C;&#x8BE5;&#x5C5E;&#x6027;&#x6307;&#x5411; Person</li>
</ul>
<p>&#x4F7F;&#x7528;&#x6784;&#x9020;&#x51FD;&#x6570;&#x6A21;&#x5F0F;&#x7684;&#x7F3A;&#x70B9;&#xFF1A;&#x6BCF;&#x4E2A;&#x65B9;&#x6CD5;&#x90FD;&#x8981;&#x5728;&#x6BCF;&#x4E2A;&#x5B9E;&#x4F8B;&#x4E0A;&#x521B;&#x5EFA;&#x4E00;&#x904D;&#x3002;&#x5982; p1 &#x548C; p2 &#x90FD;&#x6709;&#x4E00;&#x4E2A;&#x540D;&#x4E3A; sayName() &#x65B9;&#x6CD5;&#xFF0C;&#x4F46;&#x662F;&#x4E24;&#x4E2A;&#x65B9;&#x6CD5;&#x4E0D;&#x662F;&#x540C;&#x4E00;&#x4E2A; Function &#x7684;&#x5B9E;&#x4F8B;&#x3002;&#x521B;&#x5EFA;&#x4E24;&#x4E2A;&#x5B8C;&#x6210;&#x76F8;&#x540C;&#x4EFB;&#x52A1;&#x7684; Function &#x5B9E;&#x4F8B;&#x6CA1;&#x6709;&#x5FC5;&#x8981;&#xFF0C;&#x8FD9;&#x6837;&#x4F1A;&#x6D6A;&#x8D39;&#x5185;&#x5B58;&#x3002;</p>
<h3 id="&#x539F;&#x578B;&#x6A21;&#x5F0F;"><a href="#&#x539F;&#x578B;&#x6A21;&#x5F0F;" class="headerlink" title="&#x539F;&#x578B;&#x6A21;&#x5F0F;"></a>&#x539F;&#x578B;&#x6A21;&#x5F0F;</h3><p>&#x539F;&#x578B;&#x5BF9;&#x8C61;&#x7684;&#x7528;&#x9014;&#x662F;&#x5305;&#x542B;&#x53EF;&#x4EE5;&#x7531;&#x7279;&#x5B9A;&#x7C7B;&#x578B;&#x7684;&#x6240;&#x6709;&#x5B9E;&#x4F8B;&#x5171;&#x4EAB;&#x7684;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;&#x3002;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">Person</span>(<span class="params"></span>) </span>{}</div><div class="line">Person.prototype.name = <span class="string">&quot;Hiraku&quot;</span>;</div><div class="line">Person.prototype.age = <span class="number">22</span>;</div><div class="line">Person.prototype.job = <span class="string">&quot;JavaScript Engineer&quot;</span>;</div><div class="line">Person.prototype.sayName = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>{</div><div class="line">  <span class="built_in">console</span>.log(<span class="keyword">this</span>.name);</div><div class="line">}</div><div class="line"><span class="keyword">var</span> p1 = <span class="keyword">new</span> Person();</div><div class="line">p1.sayName();</div></pre></td></tr></table></figure>
<p>&#x597D;&#x5904;&#xFF1A;&#x53EF;&#x4EE5;&#x8BA9;&#x6240;&#x6709;&#x5BF9;&#x8C61;&#x5B9E;&#x4F8B;&#x5171;&#x4EAB;&#x5B83;&#x6240;&#x5305;&#x542B;&#x7684;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;&#x3002;</p>
<p>&#x5F53;&#x7ED9;&#x5BF9;&#x8C61;&#x7684;&#x5B9E;&#x4F8B;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x5C5E;&#x6027;&#x65F6;&#xFF0C;&#x8FD9;&#x4E2A;&#x5C5E;&#x6027;&#x4F1A;&#x5C4F;&#x853D;&#x539F;&#x578B;&#x5BF9;&#x8C61;&#x4E2D;&#x4FDD;&#x5B58;&#x7684;&#x540C;&#x540D;&#x5C5E;&#x6027;&#x3002;</p>
<h4 id="&#x66F4;&#x7B80;&#x5355;&#x7684;&#x539F;&#x578B;&#x8BED;&#x6CD5;"><a href="#&#x66F4;&#x7B80;&#x5355;&#x7684;&#x539F;&#x578B;&#x8BED;&#x6CD5;" class="headerlink" title="&#x66F4;&#x7B80;&#x5355;&#x7684;&#x539F;&#x578B;&#x8BED;&#x6CD5;"></a>&#x66F4;&#x7B80;&#x5355;&#x7684;&#x539F;&#x578B;&#x8BED;&#x6CD5;</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">Person</span>(<span class="params"></span>)</span>{}</div><div class="line">Person.prototype = {</div><div class="line">  <span class="attr">name</span>: <span class="string">&quot;Hiraku&quot;</span>,</div><div class="line">  <span class="attr">age</span>: <span class="number">23</span>,</div><div class="line">  <span class="attr">job</span>: <span class="string">&quot;JavaScript Engineer&quot;</span>,</div><div class="line">  <span class="attr">sayName</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>{</div><div class="line">    <span class="built_in">console</span>.log(<span class="keyword">this</span>.name);</div><div class="line">  }</div><div class="line">};</div></pre></td></tr></table></figure>
<p>&#x8FD9;&#x65F6; constructor &#x5C5E;&#x6027;&#x4E0D;&#x518D;&#x6307;&#x5411; Person &#x4E86;&#xFF0C;constructor &#x53D8;&#x6210;&#x4E86;&#x65B0;&#x5BF9;&#x8C61;&#x7684; constructor&#xFF0C;&#x662F; Object &#x6784;&#x9020;&#x51FD;&#x6570;&#x3002;</p>
<p>&#x8FD9;&#x65F6;&#xFF0C;&#x9700;&#x8981;&#x8FD8;&#x539F;&#x6784;&#x9020;&#x5668;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">Person</span>(<span class="params"></span>)</span>{}</div><div class="line">Person.prototype = {</div><div class="line">  <span class="attr">constructor</span>: Person,</div><div class="line">  <span class="attr">name</span>: <span class="string">&quot;Hiraku&quot;</span>,</div><div class="line">  <span class="attr">age</span>: <span class="number">23</span>,</div><div class="line">  <span class="attr">job</span>: <span class="string">&quot;JavaScript Engineer&quot;</span>,</div><div class="line">  <span class="attr">sayName</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>{</div><div class="line">    <span class="built_in">console</span>.log(<span class="keyword">this</span>.name);</div><div class="line">  }</div><div class="line">};</div></pre></td></tr></table></figure>
<p>&#x6CE8;&#xFF1A; &#x4EE5;&#x4E0A;&#x65B9;&#x5F0F;&#x91CD;&#x7F6E; constructor &#x5C5E;&#x6027;&#x4F1A;&#x5BFC;&#x81F4;&#x5B83;&#x7684; [[enumerable]] &#x7279;&#x6027;&#x88AB;&#x8BBE;&#x7F6E;&#x4E3A; true&#x3002; &#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x539F;&#x751F;&#x7684; constructor &#x5C5E;&#x6027;&#x662F;&#x4E0D;&#x53EF;&#x679A;&#x4E3E;&#x7684;&#xFF0C;&#x56E0;&#x6B64;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; ECMAScript5 &#x4E2D;&#x7684; Object.defineProperty() &#x65B9;&#x6CD5;&#x6765;&#x8BBE;&#x7F6E;&#x3002;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">Object</span>.defineProperty(Person.prototype, <span class="string">&quot;constructor&quot;</span>, {</div><div class="line">  <span class="attr">enumerable</span>: <span class="literal">false</span>,</div><div class="line">  <span class="attr">value</span>: Person</div><div class="line">});</div></pre></td></tr></table></figure>
<h4 id="&#x539F;&#x578B;&#x7684;&#x52A8;&#x6001;&#x7279;&#x6027;"><a href="#&#x539F;&#x578B;&#x7684;&#x52A8;&#x6001;&#x7279;&#x6027;" class="headerlink" title="&#x539F;&#x578B;&#x7684;&#x52A8;&#x6001;&#x7279;&#x6027;"></a>&#x539F;&#x578B;&#x7684;&#x52A8;&#x6001;&#x7279;&#x6027;</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">Person</span>(<span class="params"></span>) </span>{}</div><div class="line"><span class="keyword">var</span> friend = <span class="keyword">new</span> Person();</div><div class="line">Person.prototype = {</div><div class="line">  <span class="attr">name</span>: <span class="string">&quot;Hiraku&quot;</span>,</div><div class="line">  <span class="attr">sayName</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>{</div><div class="line">    <span class="built_in">console</span>.log(<span class="keyword">this</span>.name);</div><div class="line">  }</div><div class="line">}</div><div class="line">friend.sayName();  <span class="comment">// &#x62A5;&#x9519;</span></div></pre></td></tr></table></figure>
<p>&#x539F;&#x56E0;&#x662F;&#x91CD;&#x5199;&#x4E86;&#x539F;&#x578B;&#x5BF9;&#x8C61;&#xFF0C;&#x628A;&#x539F;&#x578B;&#x5BF9;&#x8C61;&#x4FEE;&#x6539;&#x4E3A;&#x53E6;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x5C31;&#x7B49;&#x4E8E;&#x5207;&#x65AD;&#x4E86;&#x6784;&#x9020;&#x51FD;&#x6570;&#x4E0E;&#x6700;&#x521D;&#x539F;&#x578B;&#x4E4B;&#x95F4;&#x7684;&#x8054;&#x7CFB;&#x3002; &#x5B9E;&#x4F8B;&#x4E2D;&#x7684;&#x6307;&#x9488;&#x4EC5;&#x4EC5;&#x6307;&#x5411;&#x539F;&#x578B;&#xFF0C;&#x800C;&#x4E0D;&#x6307;&#x5411;&#x6784;&#x9020;&#x51FD;&#x6570;&#x3002;</p>
<h4 id="&#x539F;&#x751F;&#x7684;&#x5BF9;&#x8C61;&#x539F;&#x578B;"><a href="#&#x539F;&#x751F;&#x7684;&#x5BF9;&#x8C61;&#x539F;&#x578B;" class="headerlink" title="&#x539F;&#x751F;&#x7684;&#x5BF9;&#x8C61;&#x539F;&#x578B;"></a>&#x539F;&#x751F;&#x7684;&#x5BF9;&#x8C61;&#x539F;&#x578B;</h4><p>&#x539F;&#x578B;&#x6A21;&#x5F0F;&#x4E0D;&#x4EC5;&#x4EC5;&#x4F53;&#x73B0;&#x5728;&#x521B;&#x5EFA;&#x81EA;&#x5B9A;&#x4E49;&#x7C7B;&#x578B;&#x653E;&#x6CA1;&#x653E;&#xFF0C;&#x5C31;&#x8FDE;&#x6240;&#x6709;&#x539F;&#x751F;&#x7684;&#x5F15;&#x7528;&#x7C7B;&#x578B;&#xFF0C;&#x90FD;&#x91C7;&#x7528;&#x8FD9;&#x79CD;&#x6A21;&#x5F0F;&#x521B;&#x5EFA;&#x7684;&#x3002;&#x6240;&#x6709;&#x539F;&#x751F;&#x539F;&#x751F;&#x5F15;&#x7528;&#x7C7B;&#x578B;&#x90FD;&#x5728;&#x5176;&#x6784;&#x9020;&#x51FD;&#x6570;&#x7684;&#x539F;&#x578B;&#x4E0A;&#x5B9A;&#x4E49;&#x4E86;&#x65B9;&#x6CD5;&#x3002;</p>
<p>&#x901A;&#x8FC7;&#x539F;&#x751F;&#x539F;&#x578B;&#x5BF9;&#x8C61;&#xFF0C;&#x4E0D;&#x4EC5;&#x53EF;&#x4EE5;&#x53D6;&#x5F97;&#x9ED8;&#x8BA4;&#x65B9;&#x6CD5;&#x7684;&#x5F15;&#x7528;&#xFF0C;&#x800C;&#x4E14;&#x8FD8;&#x53EF;&#x4EE5;&#x5B9A;&#x4E49;&#x65B0;&#x65B9;&#x6CD5;&#x3002;&#x53EF;&#x4EE5;&#x50CF;&#x4FEE;&#x6539;&#x81EA;&#x5B9A;&#x4E49;&#x5BF9;&#x8C61;&#x7684;&#x539F;&#x578B;&#x4E00;&#x6837;&#x4FEE;&#x6539;&#x539F;&#x751F;&#x5BF9;&#x8C61;&#x7684;&#x539F;&#x578B;&#xFF0C;&#x56E0;&#x6B64;&#x53EF;&#x4EE5;&#x968F;&#x65F6;&#x6DFB;&#x52A0;&#x65B9;&#x6CD5;&#x3002;</p>
<p>&#x4F46;&#x662F;&#xFF0C;&#x4E0D;&#x5EFA;&#x8BAE;&#x5728;&#x7F16;&#x7A0B;&#x65F6;&#x4FEE;&#x6539;&#x539F;&#x751F;&#x5BF9;&#x8C61;&#x7684;&#x539F;&#x578B;&#x3002;</p>
<h4 id="&#x539F;&#x578B;&#x6A21;&#x5F0F;&#x7684;&#x7F3A;&#x70B9;"><a href="#&#x539F;&#x578B;&#x6A21;&#x5F0F;&#x7684;&#x7F3A;&#x70B9;" class="headerlink" title="&#x539F;&#x578B;&#x6A21;&#x5F0F;&#x7684;&#x7F3A;&#x70B9;"></a>&#x539F;&#x578B;&#x6A21;&#x5F0F;&#x7684;&#x7F3A;&#x70B9;</h4><p>&#x7701;&#x7565;&#x4E86;&#x6784;&#x9020;&#x51FD;&#x6570;&#x521D;&#x59CB;&#x5316;&#xFF0C;&#x6240;&#x6709;&#x5B9E;&#x4F8B;&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#x90FD;&#x5C06;&#x53D6;&#x5F97;&#x76F8;&#x540C;&#x7684;&#x5C5E;&#x6027;&#x503C;&#x3002;&#x8FD8;&#x6709;&#xFF0C;&#x539F;&#x578B;&#x6A21;&#x5F0F;&#x7684;&#x6700;&#x5927;&#x95EE;&#x9898;&#x662F;&#x7531;&#x5176;&#x5171;&#x4EAB;&#x7684;&#x672C;&#x6027;&#x6240;&#x5BFC;&#x81F4;&#x3002;</p>
<h3 id="&#x7EC4;&#x5408;&#x4F7F;&#x7528;&#x6784;&#x9020;&#x51FD;&#x6570;&#x6A21;&#x5F0F;&#x548C;&#x539F;&#x578B;&#x6A21;&#x5F0F;"><a href="#&#x7EC4;&#x5408;&#x4F7F;&#x7528;&#x6784;&#x9020;&#x51FD;&#x6570;&#x6A21;&#x5F0F;&#x548C;&#x539F;&#x578B;&#x6A21;&#x5F0F;" class="headerlink" title="&#x7EC4;&#x5408;&#x4F7F;&#x7528;&#x6784;&#x9020;&#x51FD;&#x6570;&#x6A21;&#x5F0F;&#x548C;&#x539F;&#x578B;&#x6A21;&#x5F0F;"></a>&#x7EC4;&#x5408;&#x4F7F;&#x7528;&#x6784;&#x9020;&#x51FD;&#x6570;&#x6A21;&#x5F0F;&#x548C;&#x539F;&#x578B;&#x6A21;&#x5F0F;</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">Person</span>(<span class="params">name, age, job</span>) </span>{</div><div class="line">  <span class="keyword">this</span>.name = name;</div><div class="line">  <span class="keyword">this</span>.age = age;</div><div class="line">  <span class="keyword">this</span>.job = job;</div><div class="line">  <span class="keyword">this</span>.friends = [<span class="string">&quot;Herschal&quot;</span>, <span class="string">&quot;Camile&quot;</span>];</div><div class="line">}</div><div class="line">Person.prototype = {</div><div class="line">  <span class="attr">constructor</span>: Person,</div><div class="line">  <span class="attr">sayName</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>{</div><div class="line">    <span class="built_in">console</span>.log(<span class="keyword">this</span>.name);</div><div class="line">  }</div><div class="line">}</div><div class="line"><span class="keyword">var</span> p1 = <span class="keyword">new</span> Person(<span class="string">&quot;Hiraku&quot;</span>, <span class="number">23</span>, <span class="string">&quot;JavaScript Engineer&quot;</span>);</div><div class="line"><span class="keyword">var</span> p2 = <span class="keyword">new</span> Person(<span class="string">&quot;Wang&quot;</span>, <span class="number">22</span>, <span class="string">&quot;Java Engineer&quot;</span>);</div><div class="line">p1.friends.push(<span class="string">&quot;Van&quot;</span>);</div><div class="line"><span class="built_in">console</span>.log(p1.friends);  <span class="comment">// &quot;Herschal, Camile, Van&quot;</span></div><div class="line"><span class="built_in">console</span>.log(p2.friends);  <span class="comment">// &quot;Herschal, Camile&quot;</span></div><div class="line"><span class="built_in">console</span>.log(p1.friends === p2.friends);  <span class="comment">// false</span></div><div class="line"><span class="built_in">console</span>.log(p1.sayName === p2.sayName);  <span class="comment">// true</span></div><div class="line">p1.sayName();</div><div class="line">p2.sayName();</div></pre></td></tr></table></figure>
<p>&#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#x662F;&#x4F7F;&#x7528;&#x6700;&#x5E7F;&#x6CDB;&#x7684;&#x3002;</p>
<h3 id="&#x52A8;&#x6001;&#x539F;&#x578B;&#x6A21;&#x5F0F;"><a href="#&#x52A8;&#x6001;&#x539F;&#x578B;&#x6A21;&#x5F0F;" class="headerlink" title="&#x52A8;&#x6001;&#x539F;&#x578B;&#x6A21;&#x5F0F;"></a>&#x52A8;&#x6001;&#x539F;&#x578B;&#x6A21;&#x5F0F;</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">Person</span>(<span class="params">name, age, job</span>) </span>{</div><div class="line">  <span class="keyword">this</span>.name = name;</div><div class="line">  <span class="keyword">this</span>.age = age;</div><div class="line">  <span class="keyword">this</span>.job = job;</div><div class="line">  <span class="keyword">this</span>.friends = [<span class="string">&quot;Herschal&quot;</span>, <span class="string">&quot;Camile&quot;</span>];</div><div class="line">  <span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="keyword">this</span>.sayName != <span class="string">&quot;function&quot;</span>) {</div><div class="line">    Person.prototype.sayName = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>{</div><div class="line">      <span class="built_in">console</span>.log(<span class="keyword">this</span>.name);</div><div class="line">    };</div><div class="line">  }</div><div class="line">}</div><div class="line"><span class="keyword">var</span> p1 = <span class="keyword">new</span> Person(<span class="string">&quot;Hiraku&quot;</span>, <span class="number">23</span>, <span class="string">&quot;JavaScript Engineer&quot;</span>);</div><div class="line">p1.sayName();</div></pre></td></tr></table></figure>
<p>&#x8FD9;&#x91CC;&#x5728;&#x5BF9;&#x539F;&#x578B;&#x505A;&#x7684;&#x4FEE;&#x6539;&#xFF0C;&#x80FD;&#x7ACB;&#x5373;&#x5728;&#x6240;&#x6709;&#x5B9E;&#x4F8B;&#x4E2D;&#x5F97;&#x5230;&#x53CD;&#x6620;&#x3002;</p>
<h3 id="&#x5BC4;&#x751F;&#x6784;&#x9020;&#x51FD;&#x6570;&#x6A21;&#x5F0F;"><a href="#&#x5BC4;&#x751F;&#x6784;&#x9020;&#x51FD;&#x6570;&#x6A21;&#x5F0F;" class="headerlink" title="&#x5BC4;&#x751F;&#x6784;&#x9020;&#x51FD;&#x6570;&#x6A21;&#x5F0F;"></a>&#x5BC4;&#x751F;&#x6784;&#x9020;&#x51FD;&#x6570;&#x6A21;&#x5F0F;</h3><p>&#x57FA;&#x672C;&#x601D;&#x8DEF;&#x662F;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x8BE5;&#x51FD;&#x6570;&#x7684;&#x4F5C;&#x7528;&#x4EC5;&#x4EC5;&#x662F;&#x5C01;&#x88C5;&#x521B;&#x5EFA;&#x5BF9;&#x8C61;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x7136;&#x540E;&#x518D;&#x8FD4;&#x56DE;&#x521B;&#x5EFA;&#x7684;&#x5BF9;&#x8C61;&#x3002;</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">Person</span>(<span class="params">name, age, job</span>) </span>{</div><div class="line">  <span class="keyword">var</span> o = <span class="keyword">new</span> <span class="built_in">Object</span>();</div><div class="line">  o.name = name;</div><div class="line">  o.age = age;</div><div class="line">  o.job = job;</div><div class="line">  o.sayName = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>{</div><div class="line">    <span class="built_in">console</span>.log(<span class="keyword">this</span>.name);</div><div class="line">  };</div><div class="line">  <span class="keyword">return</span> o;</div><div class="line">}</div><div class="line"><span class="keyword">var</span> p1 = <span class="keyword">new</span> Person(<span class="string">&quot;Hiraku&quot;</span>, <span class="number">23</span>, <span class="string">&quot;JavaScript Engineer&quot;</span>);</div><div class="line">p1.sayName();</div></pre></td></tr></table></figure>
<p>&#x5173;&#x4E8E;&#x5BC4;&#x751F;&#x6784;&#x9020;&#x6A21;&#x5F0F;&#xFF0C;&#x9996;&#x5148;&#xFF0C;&#x8FD4;&#x56DE;&#x5BF9;&#x8C61;&#x4E0E;&#x6784;&#x9020;&#x51FD;&#x6570;&#x6216;&#x8005;&#x6784;&#x9020;&#x51FD;&#x6570;&#x7684;&#x539F;&#x578B;&#x5C5E;&#x6027;&#x4E4B;&#x95F4;&#x6CA1;&#x6709;&#x5173;&#x7CFB;&#x3002;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;&#x6784;&#x9020;&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x5230;&#x5BF9;&#x8C61;&#x4E0E;&#x5728;&#x6784;&#x9020;&#x51FD;&#x6570;&#x5916;&#x90E8;&#x521B;&#x5EFA;&#x7684;&#x5BF9;&#x8C61;&#x6CA1;&#x6709;&#x4EC0;&#x4E48;&#x4E0D;&#x540C;&#x3002;&#x4E0D;&#x80FD;&#x4F9D;&#x8D56; instanceof &#x64CD;&#x4F5C;&#x7B26;&#x6765;&#x786E;&#x5B9A;&#x5BF9;&#x8C61;&#x7684;&#x7C7B;&#x578B;&#x3002;</p>
<h3 id="&#x7A33;&#x59A5;&#x6784;&#x9020;&#x51FD;&#x6570;&#x6A21;&#x5F0F;"><a href="#&#x7A33;&#x59A5;&#x6784;&#x9020;&#x51FD;&#x6570;&#x6A21;&#x5F0F;" class="headerlink" title="&#x7A33;&#x59A5;&#x6784;&#x9020;&#x51FD;&#x6570;&#x6A21;&#x5F0F;"></a>&#x7A33;&#x59A5;&#x6784;&#x9020;&#x51FD;&#x6570;&#x6A21;&#x5F0F;</h3><p>&#x9996;&#x5148;&#x4ECB;&#x7ECD;&#x7A33;&#x59A5;&#x5BF9;&#x8C61;&#xFF0C;&#x7A33;&#x59A5;&#x5BF9;&#x8C61;&#x662F;&#x6307;&#x6CA1;&#x6709;&#x516C;&#x5171;&#x5C5E;&#x6027;&#xFF0C;&#x800C;&#x4E14;&#x5176;&#x5B83;&#x65B9;&#x6CD5;&#x4E5F;&#x4E0D;&#x5F15;&#x7528; this &#x7684;&#x5BF9;&#x8C61;&#x3002;&#x7A33;&#x59A5;&#x6A21;&#x5F0F;&#x9002;&#x5408;&#x5728;&#x4E00;&#x4E9B;&#x5B89;&#x5168;&#x7684;&#x73AF;&#x5883;&#x4E2D;&#xFF0C;&#x6216;&#x8005;&#x5728;&#x9632;&#x6B62;&#x6570;&#x636E;&#x88AB;&#x5176;&#x5B83;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x6539;&#x52A8;&#x65F6;&#x4F7F;&#x7528;&#x3002;&#x7A33;&#x59A5;&#x6A21;&#x5F0F;&#x9075;&#x5FAA;&#x5BC4;&#x751F;&#x6A21;&#x5F0F;&#xFF0C;&#x4F46;&#x6709;&#x4E24;&#x70B9;&#x4E0D;&#x540C;&#x3002;</p>
<ul>
<li>&#x65B0;&#x521B;&#x5EFA;&#x5BF9;&#x8C61;&#x7684;&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;&#x4E0D;&#x5F15;&#x7528; this&#xFF1B;</li>
<li>&#x4E0D;&#x4F7F;&#x7528; new &#x64CD;&#x4F5C;&#x7B26;&#x6765;&#x8C03;&#x7528;&#x6784;&#x9020;&#x51FD;&#x6570;&#x3002;</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">Person</span>(<span class="params">name, age, job</span>) </span>{</div><div class="line">  <span class="keyword">var</span> o = <span class="keyword">new</span> <span class="built_in">Object</span>();</div><div class="line">  o.sayName = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>{</div><div class="line">    <span class="built_in">console</span>.log(name);</div><div class="line">  };</div><div class="line">  <span class="keyword">return</span> o;</div><div class="line">}</div><div class="line"><span class="keyword">var</span> p1 = Person(<span class="string">&quot;Hiraku&quot;</span>, <span class="number">23</span>, <span class="string">&quot;JavaScript Engineer&quot;</span>);</div><div class="line">p1.sayName();</div></pre></td></tr></table></figure>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        
  <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
    <div>感谢您的支持！</div>
    <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
      <span>打 赏</span>
    </button>
    <div id="QR" style="display: none;">
      
        <div id="wechat" style="display: inline-block">
          <img id="wechat_qr" src="http://ofy7k3v4s.bkt.clouddn.com/wcpayCode.jpg" alt="Hongqin Ma WeChat Pay"/>
          <p>微信打赏</p>
        </div>
      
      
        <div id="alipay" style="display: inline-block">
          <img id="alipay_qr" src="http://ofy7k3v4s.bkt.clouddn.com/apayCode.jpg" alt="Hongqin Ma Alipay"/>
          <p>支付宝打赏</p>
        </div>
      
    </div>
  </div>


      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/js/" rel="tag">#js</a>
          
            <a href="/tags/面向对象/" rel="tag">#面向对象</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2014/05/24/10-js-的函数/" rel="next" title="js 的函数">
                <i class="fa fa-chevron-left"></i> js 的函数
              </a>
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2014/06/14/12-原型链/" rel="prev" title="原型链">
                原型链 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
        <div class="ds-share flat" data-thread-key="2014/06/03/11-面向对象的程序设计/"
     data-title="面向对象的程序设计"
     data-content=""
     data-url="https://hongqinma.github.io/2014/06/03/11-面向对象的程序设计/">
  <div class="ds-share-inline">
    <ul  class="ds-share-icons-16">

      <li data-toggle="ds-share-icons-more"><a class="ds-more" href="javascript:void(0);">分享到：</a></li>
      <li><a class="ds-weibo" href="javascript:void(0);" data-service="weibo">微博</a></li>
      <li><a class="ds-qzone" href="javascript:void(0);" data-service="qzone">QQ空间</a></li>
      <li><a class="ds-qqt" href="javascript:void(0);" data-service="qqt">腾讯微博</a></li>
      <li><a class="ds-wechat" href="javascript:void(0);" data-service="wechat">微信</a></li>

    </ul>
    <div class="ds-share-icons-more">
    </div>
  </div>
</div>
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
      <div class="ds-thread" data-thread-key="2014/06/03/11-面向对象的程序设计/"
           data-title="面向对象的程序设计" data-url="https://hongqinma.github.io/2014/06/03/11-面向对象的程序设计/">
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            Overview
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="//schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/photo.jpg"
               alt="Hongqin Ma" />
          <p class="site-author-name" itemprop="name">Hongqin Ma</p>
          <p class="site-description motion-element" itemprop="description">Spell at all costs， to rush you in the future. </p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">68</span>
              <span class="site-state-item-name">posts</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              <a href="/categories">
                <span class="site-state-item-count">16</span>
                <span class="site-state-item-name">categories</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">57</span>
                <span class="site-state-item-name">tags</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/HongqinMa" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://twitter.com/HongqinMa" target="_blank" title="Twitter">
                  
                    <i class="fa fa-fw fa-twitter"></i>
                  
                  Twitter
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://www.facebook.com/profile.php?id=100006581058735" target="_blank" title="Facebook">
                  
                    <i class="fa fa-fw fa-facebook"></i>
                  
                  Facebook
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://www.zhihu.com/people/HirakuMa" target="_blank" title="知乎">
                  
                    <i class="fa fa-fw fa-share-alt"></i>
                  
                  知乎
                </a>
              </span>
            
          
        </div>

        
        

        
        
          <div class="links-of-blogroll motion-element links-of-blogroll-inline">
            <div class="links-of-blogroll-title">
              <i class="fa  fa-fw fa-globe"></i>
              Links
            </div>
            <ul class="links-of-blogroll-list">
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.cnblogs.com/rubylouvre/" title="司徒正美" target="_blank">司徒正美</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.cnblogs.com/sanshi/" title="三生石上" target="_blank">三生石上</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.cnblogs.com/TomXu/" title="汤姆大叔" target="_blank">汤姆大叔</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://gold.xitu.io/" title="掘金" target="_blank">掘金</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="https://segmentfault.com/" title="segmentfault" target="_blank">segmentfault</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.jianshu.com/users/1cbb94e5ed4d/latest_articles" title="简书" target="_blank">简书</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="https://home.cnblogs.com/" title="博客园" target="_blank">博客园</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="https://www.awesomes.cn/" title="Awesomes" target="_blank">Awesomes</a>
                </li>
              
            </ul>
          </div>
        

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">
            
              
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#理解对象"><span class="nav-number">1.</span> <span class="nav-text">理解对象</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#构造函数和类的标识"><span class="nav-number">2.</span> <span class="nav-text">构造函数和类的标识</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#constructor-属性"><span class="nav-number">3.</span> <span class="nav-text">constructor 属性</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#创建对象的几种方式"><span class="nav-number">4.</span> <span class="nav-text">创建对象的几种方式</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#工厂模式"><span class="nav-number">4.1.</span> <span class="nav-text">工厂模式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#构造函数模式"><span class="nav-number">4.2.</span> <span class="nav-text">构造函数模式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#原型模式"><span class="nav-number">4.3.</span> <span class="nav-text">原型模式</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#更简单的原型语法"><span class="nav-number">4.3.1.</span> <span class="nav-text">更简单的原型语法</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#原型的动态特性"><span class="nav-number">4.3.2.</span> <span class="nav-text">原型的动态特性</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#原生的对象原型"><span class="nav-number">4.3.3.</span> <span class="nav-text">原生的对象原型</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#原型模式的缺点"><span class="nav-number">4.3.4.</span> <span class="nav-text">原型模式的缺点</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#组合使用构造函数模式和原型模式"><span class="nav-number">4.4.</span> <span class="nav-text">组合使用构造函数模式和原型模式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#动态原型模式"><span class="nav-number">4.5.</span> <span class="nav-text">动态原型模式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#寄生构造函数模式"><span class="nav-number">4.6.</span> <span class="nav-text">寄生构造函数模式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#稳妥构造函数模式"><span class="nav-number">4.7.</span> <span class="nav-text">稳妥构造函数模式</span></a></li></ol></li></ol></div>
            
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2016</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Hongqin Ma</span>
</div>

<div class="powered-by">
  Powered by <a class="theme-link" href="https://hexo.io">Hexo</a>
</div>

<div class="theme-info">
  Theme -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>

<script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<span id="busuanzi_container_site_pv">
    &nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;Page Views: <span id="busuanzi_value_site_pv"></span> 
</span>

        

<div class="busuanzi-count">

  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv"><i class="fa fa-user"></i><span class="busuanzi-value" id="busuanzi_value_site_uv"></span></span>
  

  
    <span class="site-pv"><i class="fa fa-eye"></i><span class="busuanzi-value" id="busuanzi_value_site_pv"></span></span>
  
  
</div>



        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.2"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.2"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.2"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.2"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.2"></script>



  

  
    
  

  <script type="text/javascript">
    var duoshuoQuery = {short_name:"Hiraku"};
    (function() {
      var ds = document.createElement('script');
      ds.type = 'text/javascript';ds.async = true;
      ds.id = 'duoshuo-script';
      ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
      ds.charset = 'UTF-8';
      (document.getElementsByTagName('head')[0]
      || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
  </script>

  
    
    <script src="/lib/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>
    <script src="/js/src/hook-duoshuo.js"></script>
  






  
  

  

  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.1.js"></script>
  <script>AV.initialize("PPmoIQxeu8Sd21h6Tl9TthNx-gzGzoHsz", "rTvbY2H7DvWzRipiBwn72RQ4");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  

  
  
  <!-- custom analytics part create by xiamo -->
<script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.1.js"></script>
<script>AV.initialize("PPmoIQxeu8Sd21h6Tl9TthNx-gzGzoHsz", "rTvbY2H7DvWzRipiBwn72RQ4");</script>
<script>
function showTime(Counter) {
  var query = new AV.Query(Counter);
  $(".leancloud_visitors").each(function() {
    var url = $(this).attr("id").trim();
    query.equalTo("url", url);
    query.find({
      success: function(results) {
        if (results.length == 0) {
          var content = '0 ' + $(document.getElementById(url)).text();
          $(document.getElementById(url)).text(content);
          return;
        }
        for (var i = 0; i < results.length; i++) {
          var object = results[i];
          var content = object.get('time') + ' ' + $(document.getElementById(url)).text();
          $(document.getElementById(url)).text(content);
        }
      },
      error: function(object, error) {
        console.log("Error: " + error.code + " " + error.message);
      }
    });

  });
}

function addCount(Counter) {
  var Counter = AV.Object.extend("Counter");
  url = $(".leancloud_visitors").attr('id').trim();
  title = $(".leancloud_visitors").attr('data-flag-title').trim();
  var query = new AV.Query(Counter);
  query.equalTo("url", url);
  query.find({
    success: function(results) {
      if (results.length > 0) {
        var counter = results[0];
        counter.fetchWhenSave(true);
        counter.increment("time");
        counter.save(null, {
          success: function(counter) {
            var content =  counter.get('time') + ' ' + $(document.getElementById(url)).text();
            $(document.getElementById(url)).text(content);
          },
          error: function(counter, error) {
            console.log('Failed to save Visitor num, with error message: ' + error.message);
          }
        });
      } else {
        var newcounter = new Counter();
        newcounter.set("title", title);
        newcounter.set("url", url);
        newcounter.set("time", 1);
        newcounter.save(null, {
          success: function(newcounter) {
              console.log("newcounter.get('time')="+newcounter.get('time'));
            var content = newcounter.get('time') + ' ' + $(document.getElementById(url)).text();
            $(document.getElementById(url)).text(content);
          },
          error: function(newcounter, error) {
            console.log('Failed to create');
          }
        });
      }
    },
    error: function(error) {
      console.log('Error:' + error.code + " " + error.message);
    }
  });
}
$(function() {
  var Counter = AV.Object.extend("Counter");
  if ($('.leancloud_visitors').length == 1) {
    addCount(Counter);
  } else if ($('.post-title-link').length > 1) {
    showTime(Counter);
  }
}); 
</script>

  
</body>
</html>
